﻿using nplJobsCommon.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nplJobsDAL.Select
{
    /// <summary>
    /// Select countries from the database
    /// </summary>
   public class SelectCountry: DataAccessBase
    {
       public SelectCountry()
        {
            StoredProcedureName = StoredProcedure.Name.spSelectCountry.ToString();
        }

         #region GetCountry
         public List<Country> GetCountry()
        {
            DataBaseHelper dbHelper;
            DataSet dsCountry;

            try
            {
                dbHelper = new DataBaseHelper(StoredProcedureName);
                dsCountry = dbHelper.Run(base.ConnectionString);
                return GetList(dsCountry);
            }
            catch (Exception)
            {

                throw;
            }
        } 
        #endregion

         #region GetList
         private List<Country> GetList(DataSet dsCountry)
        {
            List<Country> countries;
            try
            {
                countries = (from n in dsCountry.Tables[0].AsEnumerable()
                             select new Country
                                 {
                                     ID = n.Field<int>("ID"),
                                     Name = n.Field<string>("Name")
                                 }).ToList();

                return countries;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally { }
        } 
        #endregion
    }
}
